package com.shop.shoppingmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.shop.shoppingmall.dao.ShIntegralDao;
import com.shop.shoppingmall.entity.ShIntegral;
import com.shop.shoppingmall.service.ShIntegralService;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 积分明细表(ShIntegral)表服务实现类
 *
 * @author makejava
 * @since 2020-09-11 10:59:26
 */
@Service("shIntegralService")
@Transactional
public class ShIntegralServiceImpl implements ShIntegralService {
    @Resource
    private ShIntegralDao shIntegralDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShIntegral queryById(String id) {
        return this.shIntegralDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param userId 实例对象
     * @return 实例对象
     */
    @Override
    public Result insertInCome(BigDecimal income,String userId,String frUserId,String detail) {
        ShIntegral shIntegral = new ShIntegral();
        shIntegral.setId(UUIDGenerator.generate());
        shIntegral.setIncome(income);
        shIntegral.setUserId(userId);
        shIntegral.setFrUserId(frUserId);
        shIntegral.setDetail(detail);
        shIntegral.setType("1");
        this.shIntegralDao.insert(shIntegral);
        return Result.ok();
    }

    /**
     * 添加积分支出明细纪录
     * @param expenditure 支出金额
     * @param userId  用户id
     * @param detail  备注
     * @return
     */
    @Override
    public Result insertExpenditure(BigDecimal expenditure,String userId,String detail) {
        ShIntegral shIntegral = new ShIntegral();
        shIntegral.setId(UUIDGenerator.generate());
        shIntegral.setExpenditure(expenditure);
        shIntegral.setUserId(userId);
        shIntegral.setDetail(detail);
        shIntegral.setType("2");
        this.shIntegralDao.insert(shIntegral);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param shIntegral 实例对象
     * @return 实例对象
     */
    @Override
    public ShIntegral update(ShIntegral shIntegral) {
        this.shIntegralDao.update(shIntegral);
        return this.queryById(shIntegral.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.shIntegralDao.deleteById(id) > 0;
    }

    /**
     * 查询全部
     * @param map
     * @return
     */
     @Override
    public Result queryAll(Map map) {
         List<ShIntegral> integralList = shIntegralDao.queryAll(map);
         return Result.ok(integralList);

     }

    @Override
    public Result selectQueryAll(Map map) {
        Integer pagenum = (Integer) map.get("pagenum");
        Integer pagesize = (Integer) map.get("pagesize");
        PageHelper.startPage(pagenum,pagesize);
        int a = (pagenum-1)*pagesize;
        map.put("offset",a);
        map.put("limit",pagesize);
        List<ShIntegral> shIntegrals = shIntegralDao.queryIntegralAll(map);
        Map map1 = new HashMap();
        if(shIntegrals == null){
            return Result.error("暂时没有积分明细的信息！");
        }
        ShIntegral shIntegral = shIntegralDao.selectCountAll(map);
        map1.put("total", shIntegral.getSums());
        map1.put("shIntegrals",shIntegrals);
        return Result.ok(map1);
    }


}
